package com.cuitrip.apiservice;

import android.text.TextUtils;

import com.alibaba.fastjson.TypeReference;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.business.home.recommend.model.HomeCard;
import com.cuitrip.business.home.recommend.model.ListResponse;
import com.cuitrip.business.invite.model.InviteInfo;
import com.cuitrip.business.invite.model.InviteList;
import com.cuitrip.business.login.model.ThirdAccount;
import com.cuitrip.business.user.ModifyUserLanguageActivity;
import com.cuitrip.business.user.identity.IdentityMode;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.model.ForceUpdate;
import com.cuitrip.util.I118Util;
import com.lab.network.model.CtApiRequest;
import com.lab.network.model.CtRemoteBusiness;
import com.lab.network.model.ParseType;
import com.lab.network.proxy.ApiProxy;
import com.lab.rtenvironment.AppCoreRuntime;
import com.umeng.message.UmengRegistrar;

import java.util.Map;

/**
 * 用户相关网络请求
 *
 * @author ak
 */
public class UserBusiness {

    public static void login(ApiProxy apiProxy,
                             String username, String passwd) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.LOGIN);
        ctApiRequest.addParam("username", username);
        ctApiRequest.addParam("passwd", passwd);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CtUserInfo.class));
    }

    public static void register(ApiProxy apiProxy,
                                String email, String pwd, String nick) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.REGISTER);
        ctApiRequest.addParam("email", email);
        ctApiRequest.addParam("pwd", pwd);
        ctApiRequest.addParam("nick", nick);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CtUserInfo.class));
    }

    public static void logout(ApiProxy apiProxy) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.LOGOUT);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void modifyPassword(ApiProxy apiProxy,
                                      String oldPasswd, String newPasswd, String rePasswd) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.MODIFY_PASSWORD);
        if (!TextUtils.isEmpty(oldPasswd)) {
            ctApiRequest.addParam("oldPasswd", oldPasswd);
        }
        ctApiRequest.addParam("newPasswd", newPasswd);
        ctApiRequest.addParam("rePasswd", rePasswd);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void updateDeviceToken(ApiProxy apiProxy, CtUserInfo userInfo) {
        String deviceId = UmengRegistrar.getRegistrationId(AppCoreRuntime.context);
        if (TextUtils.isEmpty(deviceId)) {
            if (userInfo == null) {
                return;
            } else {
                deviceId = "emptydevicetokenuser" + userInfo.getUid() + "cannotgettoken";
            }
        }
        updateDeviceToken(apiProxy, deviceId, userInfo);
    }


    public static void updateDeviceToken(final ApiProxy apiProxy, String deviceToken, CtUserInfo userInfo) {
        final CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.UP_DEVICE_TOKEN);
        ctApiRequest.addParam("deviceToken", deviceToken);
        ctApiRequest.removeUserInfoParam();
        if (userInfo != null) {
            ctApiRequest.addParam("uid", userInfo.getUid());
            ctApiRequest.addParam("token", userInfo.getToken());
        }
        postDeviceToken(apiProxy, ctApiRequest);
        /*if (!Dexter.isRequestOngoing()) {
            Dexter.checkPermission(new PermissionListener() {

                @Override
                public void onPermissionGranted(PermissionGrantedResponse response) {
                    ctApiRequest.addParam("lat", LocationUtil.getLocation().first);
                    ctApiRequest.addParam("lng", LocationUtil.getLocation().second);
                    postDeviceToken(apiProxy, ctApiRequest);
                }

                @Override
                public void onPermissionDenied(PermissionDeniedResponse response) {
                    postDeviceToken(apiProxy, ctApiRequest);
                }

                @Override
                public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                    if (null != token){
                        token.cancelPermissionRequest();
                    }
                }

            }, Manifest.permission.ACCESS_COARSE_LOCATION);
        }*/

    }

    private static void postDeviceToken(ApiProxy apiProxy, CtApiRequest ctApiRequest) {
        ctApiRequest.addParam("country", I118Util.getLocale().toString());
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(new TypeReference<Object>() {
        }.getType()));
    }


    public static void changeType(ApiProxy apiProxy, int type) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.CHANGE_TYPE);
        ctApiRequest.addParam("type", type);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(new TypeReference<Object>() {
        }.getType()));
    }

    public static void updateProfile(ApiProxy apiProxy, Map<String, String> map) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.MODIFY_USERINFO);
        for (String key : map.keySet()) {
            ctApiRequest.addParam(key, map.get(key));
        }
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(new TypeReference<CtUserInfo>() {
        }.getType()));
    }

    public static void getUserInfo(ApiProxy apiProxy, String uid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_USER_INFO);
        ctApiRequest.addParam("otherId", uid);
        ctApiRequest.addParam("lang", I118Util.getLanguageTag());
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CtUserInfo.class));
    }

    public static void updateIndentity(ApiProxy apiProxy, IdentityMode identityMode) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.UPLOAD_CREDIT_INFO);
        ctApiRequest.addParam("idPic", identityMode.getImagesString());
        ctApiRequest.addParam("idType", identityMode.getIdentityType());
        ctApiRequest.addParam("idArea", identityMode.getCountry());
        ctApiRequest.addParam("idValidTime", identityMode.getIdentityDate());
        ctApiRequest.addParam("idNumber", identityMode.getIdentityNumber());
        ctApiRequest.addParam("realName", identityMode.getRealName());
        ctApiRequest.addParam("gender", identityMode.getGender());
        ctApiRequest.addParam("birthday", identityMode.getBirthDay());
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(new TypeReference<ListResponse<HomeCard>>() {
        }.getType()));
    }

    public static void verifyId(ApiProxy apiProxy,String idPic,String idType){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.UPLOAD_CREDIT_INFO);
        ctApiRequest.addParam("idPic", idPic);
        ctApiRequest.addParam("idType", idType);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness,new ParseType());
    }

    public static void getCash(ApiProxy apiProxy, String payPalAccount, String cashAmount, String moneyType,String userAccountId) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_CASH);
        ctApiRequest.addParam("payPalAccount", payPalAccount);
        ctApiRequest.addParam("cashAmount", cashAmount);
        ctApiRequest.addParam("moneyType", moneyType);
        ctApiRequest.addParam("userAccountId",userAccountId);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(Object.class));
    }

    public static void forceUpdate(ApiProxy apiProxy) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.VERSION_CONTROL);
        ctApiRequest.addParam("platform", "android");
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ForceUpdate.class));
    }


    public static void loginThirdAccount(ApiProxy apiProxy, ThirdAccount thirdAccount,
                                         String phoneNum,
                                         String countryCode, String vcode) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.LOGIN_THIRD_ACCOUNT);
        ctApiRequest.addParam("type", thirdAccount.accountType);
        ctApiRequest.addParam("thirdObj", thirdAccount.getThirdObject());
        ctApiRequest.addParam("mobile", phoneNum);
        ctApiRequest.addParam("countryCode", countryCode);
        ctApiRequest.addParam("vcode", vcode);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CtUserInfo.class));
    }

    public static void loginThirdAccount(ApiProxy apiProxy, ThirdAccount thirdAccount, String email) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.LOGIN_THIRD_ACCOUNT);
        ctApiRequest.addParam("type", thirdAccount.accountType);
        ctApiRequest.addParam("thirdObj", thirdAccount.getThirdObject());
        ctApiRequest.addParam("email", email);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CtUserInfo.class));
    }

    public static void bindThirdAccount(ApiProxy apiProxy, ThirdAccount thirdAccount
    ) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.BIND_ACCOUNT);
        ctApiRequest.addParam("type", thirdAccount.accountType);
        ctApiRequest.addParam("thirdObj", thirdAccount.getThirdObject());
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CtUserInfo.class));
    }


    public static void unbindThirdAccount(ApiProxy apiProxy, int accountType) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.UN_BIND_ACCOUNT);
        ctApiRequest.addParam("type", accountType);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CtUserInfo.class));
    }

    public static void sendVerifyEmail(ApiProxy apiProxy, String email) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.SEND_VERIFY_EMAIL);
        ctApiRequest.addParam("email", email);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(Object.class));
    }

    public static void verifyEmail(ApiProxy apiProxy, String email, String vcode) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.VERIFY_EMAIL);
        ctApiRequest.addParam("email", email);
        ctApiRequest.addParam("vcode", vcode);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(Object.class));
    }

    public static void sendVerifySMS(ApiProxy apiProxy, String countryCode, String mobile) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.SEND_VERIFY_SMS);
        ctApiRequest.addParam("countryCode", countryCode);
        ctApiRequest.addParam("mobile", mobile);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(Object.class));
    }

    public static void verifySMS(ApiProxy apiProxy, String countryCode, String mobile, String vcode) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.VERIFY_PHONE);
        ctApiRequest.addParam("countryCode", countryCode);
        ctApiRequest.addParam("mobile", mobile);
        ctApiRequest.addParam("vcode", vcode);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(Object.class));
    }

    public static void getLanguageList(ApiProxy apiProxy) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_LANGUAGE_LIST);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ModifyUserLanguageActivity.Languages.class));
    }

    public static void getInviteInfo(ApiProxy apiProxy){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_INVITE_INFO);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(InviteInfo.class));
    }

    public static void getInviteList(ApiProxy apiProxy){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_INVITE_LIST);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(InviteList.class));
    }
}
